package com.um.jdy.app.charge.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alipay.api.AlipayApiException;
import com.um.jdy.app.charge.entity.dto.EBStopParam;
import com.um.jdy.app.charge.entity.dto.EBStartParam;
import com.um.jdy.app.charge.manager.EBOrderManager;
import com.um.jdy.app.charge.manager.OrderManager;
import com.um.springboot.starter.annotation.ActionLogRequired;
import com.um.springboot.starter.orm.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@RestController
@RequestMapping("/api/v1/order/charge/eb")
@Api(tags = "充电订单-电单车")
@Validated
public class EBOrderController {

    @Autowired
    EBOrderManager ebOrderManager;

    @SaCheckLogin
    @ApiOperation("开始充电")
    @PostMapping("start")
    public Response start(@RequestBody @Valid EBStartParam param) {
        return Response.success(ebOrderManager.startCharge(param));
    }

    @SaCheckLogin
    @ApiOperation("取消充电")
    @PostMapping("cancel")
    public Response cancel(@RequestBody @Valid EBStopParam param) throws Exception {
        ebOrderManager.checkUserOrderExits(param.getOrderNo());
        ebOrderManager.cancelCharge(param.getOrderNo());
        return Response.success();
    }

    @SaCheckLogin
    @ApiOperation("结束充电")
    @PostMapping("stop")
    public Response stop(@RequestBody @Valid EBStopParam param) throws Exception {
        ebOrderManager.checkUserOrderExits(param.getOrderNo());
        ebOrderManager.stopCharge(param.getOrderNo());
        return Response.success();
    }

    @SaCheckLogin
    @ApiOperation("结束离线设备充电")
    @PostMapping("stop_offline")
    public Response stopOffline(@RequestBody @Valid EBStopParam param) throws Exception {
        ebOrderManager.checkUserOrderExits(param.getOrderNo());
        ebOrderManager.stopOfflineCharge(param.getOrderNo());
        return Response.success();
    }


    @ActionLogRequired
    @RequestMapping("wxpay/notify/{com_id}")
    public Response wxPayNotify(HttpServletRequest request, @PathVariable String com_id) {
        ebOrderManager.wxPayNotify(request, com_id);
        return Response.success();
    }

    @ActionLogRequired
    @RequestMapping(value = "alipay/notify")
    @ResponseBody
    public String alipayNotify(HttpServletRequest request) throws AlipayApiException {
        ebOrderManager.aliPayNotify(request);
        return "success";
    }

}
